The VectorScript IF statement evaluates a
BOOLEAN control expression and executes a controlled statement only if the expression evaluates to
TRUE.
IF statements can also be optionally written to execute a second statement if the control expression evaluates to
FALSE.
When an IF statement executes, the control expression is evaluated to obtain a
BOOLEAN result. If the result is
TRUE, the statement after the
THEN keyword is executed and the
IF statement is exited. If the expression evaluates to
FALSE, the statement is skipped unless the
ELSE keyword and a statement are encountered. In this case, the statement after the
ELSE keyword is executed. For example:
If the value in i is even, then the expression
i MOD 2 will evaluate to
TRUE and the statement
Message('Even value') will be executed. If the value of
i is odd, then
Message('Odd value') will be executed.
Note that the statement contained between the THEN and
ELSE keywords does not require a semi-colon after it; in this case the
ELSE keyword indicates the end of the statement. If the
ELSE keyword were omitted, a semicolon would be required.
Like other statements, the IF statement supports the use of a compound statement as the controlled statement.
IF statements can also be nested; that is, the statement following the
THEN keyword may also be an
IF statement. Nesting allows you to construct statements which can take actions based on the results of several mutually exclusive conditions.
Nested IF statements can rapidly become confusing:
If the matching of IF and
THEN becomes confusing, you can clarify the source code by using compound statements or by applying indentation and comments: